package com.hdoit.ares.web.official.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifyTree;
import com.hdoit.ares.selfcommon.enums.official.ArticalClassifyType;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月08日 17:13:21
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/official/articalClassify/")
public class ArticalClassifyController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/official/articalClassify/";
    }

    private IArticalClassifyService articalClassifyService;
    public IArticalClassifyService getArticalClassifyService() {
        return articalClassifyService;
    }
    @Resource
    public void setArticalClassifyService(IArticalClassifyService articalClassifyService) {
        this.articalClassifyService = articalClassifyService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="articalClassifyDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String menuJsonViewName = "menuJson";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model){
        return view(indexViewName);
    }
    
    @RequestMapping(value = menuJsonViewName, method = { RequestMethod.GET },produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String menuJson(){
		List<ArticalClassifyTree> menuTreeList=articalClassifyService.queryMenuTreeList();
		
		String menuJson=JsonUtils.writeValueAsString(menuTreeList);
		
		return menuJson;
	}
    

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String articalClassifyDetailed(Model model,String key,String parentId){
        ArticalClassify articalClassify=new ArticalClassify();
        articalClassify.setParentId(parentId);
        articalClassify.setType(ArticalClassifyType.top);
        articalClassify.setHidden(YesOrNoInteger.no);
        if(!StringUtils.isEmpty(key)){
            articalClassify=articalClassifyService.queryModel(key);
        }
        model.addAttribute("articalClassify", articalClassify);
        return view(detailedViewName);
    }
    

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ArticalClassify articalClassify){
        if(StringUtils.isEmpty(articalClassify.getId())){
            articalClassify.setId(CommonUtils.getKeyId());
            articalClassify.setCreateDate(DateUtils.getCurrent());
            articalClassify.setDeleteMark(YesOrNo.no);
            articalClassify.setCreateUserId(getLoginModel().getLoginId());
            articalClassify.setCreateUserName(getLoginModel().getLoginName());
            articalClassify.setModelStatus(ModelStatus.add);
        }else{
            articalClassify.setModifyDate(DateUtils.getCurrent());
            articalClassify.setModifyUserId(getLoginModel().getLoginId());
            articalClassify.setModifyUserName(getLoginModel().getLoginName());
            articalClassify.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(articalClassifyService.applyChange(articalClassify)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("articalClassify", articalClassify);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.GET,RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(articalClassifyService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}